﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

using System.Data;
using System.Text;
using System.Collections;
using Newtonsoft.Json;

namespace TRPOP.CMS.Web.Admin
{
    public partial class admin_setting : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_Other;
        }
        #endregion

        #region 页面变量
        protected int m_cid = 0;
        ParamTypeManage oParamTypeManage = new ParamTypeManage();
        protected string m_linkStr = "";
        private string m_varPre = "[$";
        private string m_varAppend = "]";
        protected string m_title = "系统设置";
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
            LoadAllLink();
        }

        //批量修改
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            foreach (string tempelement in Request.Form.AllKeys)
            {
                if (tempelement.IndexOf("_VarValue") > 0)
                {
                    SystemVarInfo oSystemVarInfo = new SystemVarInfo(
                        tempelement.Replace("_VarValue", ""),
                        Request.Form[tempelement.Replace("_VarValue", "_VarName")].ToString(),
                        Request.Form[tempelement].ToString(),
                        string.Empty, string.Empty);

                    SystemVarInfoHelper.SaveVarByKey(oSystemVarInfo, ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).KeyWord, true);
                }
            }

            base.SaveLog(Plugin.LogKeys.OperObject.SysOper, Plugin.LogKeys.OperResult.Success);

            SubmitAlertAndThisNewLocation(EnumSubmitResult.Success, "admin_setting.aspx?cid=" + ((ParamTypeInfo)GetPageViewState("ParamTypeInfo")).ParamTypeId.ToString());
        }

        #endregion

        #region 页面方法


        public void LoadBase()
        {
            DelParam();//如果是删除操作，则执行

            int.TryParse(InitRequestViewState("cid"), out m_cid);

            //初始化textbox等的数据
            if (m_cid == 0)
            {
                m_cid = 1;
                InitPageViewState("cid", "1");
            }
            else if (m_cid == 2)
            {
                m_title = "模板变量管理";
            }

            ParamTypeInfo oParamTypeInfo = oParamTypeManage.GetInfoById(m_cid);

            InitPageViewState("ParamTypeInfo", oParamTypeInfo);
            if (GetPageViewState("ParamTypeInfo") != null)
            {
                LoadFormItem((ParamTypeInfo)GetPageViewState("ParamTypeInfo"));
            }
            else
            {
                Alert("请通过正确的路径访问页面");
                return;
            }
        }

        //加载html元素
        public void LoadFormItem(ParamTypeInfo oParamTypeInfo)
        {
            string strReg = ltFormItem.Text;
            StringBuilder strb = new StringBuilder();
            
            if (oParamTypeInfo != null)
            {

                foreach (SystemVarInfo oSystemVarInfo in SystemVarInfoHelper.GetVarsByParamKey(oParamTypeInfo.KeyWord))
                {
                    strb.Append(creatOneItem(oSystemVarInfo.VarType, oSystemVarInfo, oParamTypeInfo));
                    strb.Append(System.Environment.NewLine);
                }

                ltFormItem.Text = strb.ToString();
            }
            else
            {
                ltFormItem.Text = string.Empty;
            }
        }

        //加载一个html元素
        public string creatOneItem(string ovarType, SystemVarInfo oSystemVarInfo, ParamTypeInfo oParamTypeInfo)
        {
            string returnstr = string.Empty;

            string varName = m_varPre + oSystemVarInfo.VarKey + m_varAppend;

            string strShowName = oSystemVarInfo.VarName + FormAssist.Addhidden(oSystemVarInfo.VarKey + "_VarName", oSystemVarInfo.VarKey + "_VarName", oSystemVarInfo.VarName);

            string strDel = GetDelString(oParamTypeInfo.ParamTypeId.ToString(), oSystemVarInfo.VarKey, oSystemVarInfo.VarDelType);

            string strSuper = "<a  href=\"Param_save.aspx?cid=" + oParamTypeInfo.ParamTypeId.ToString() + "&VarKey=" + Microsoft.JScript.GlobalObject.escape(oSystemVarInfo.VarKey) + "\">高级</a>&nbsp;";

            switch (ovarType)
            {
                case "textarea":
                    returnstr = string.Format(
                         ltFormItem.Text,
                         strShowName,
                         FormAssist.Addtextarea(oSystemVarInfo.VarKey + "_VarValue",
                                                oSystemVarInfo.VarKey + "_Id",
                                                4,
                                                20,
                                                oSystemVarInfo.VarValue, null),
                         varName,
                         strSuper + strDel
                        );
                    break;
                case "radio":
                    List<JavaScriptObject> lst = new List<JavaScriptObject>();
                    JavaScriptObject ojsonobjTrue = new JavaScriptObject();
                    ojsonobjTrue.Add(HtmlAttributeKey.RadioShowName, "是");
                    ojsonobjTrue.Add(HtmlAttributeKey.RadioValue, "true");
                    JavaScriptObject ojsonobjFalse = new JavaScriptObject();
                    ojsonobjFalse.Add(HtmlAttributeKey.RadioShowName, "否");
                    ojsonobjFalse.Add(HtmlAttributeKey.RadioValue, "false");
                    if (oSystemVarInfo.VarValue.ToLower() == "true")
                    {
                        ojsonobjTrue.Add(HtmlAttributeKey.Radiochecked, "true");
                    }
                    else
                        ojsonobjFalse.Add(HtmlAttributeKey.Radiochecked, "true");
                    lst.Add(ojsonobjTrue);
                    lst.Add(ojsonobjFalse);
                    returnstr = string.Format(
                         ltFormItem.Text,
                         strShowName,
                         FormAssist.Addradion(oSystemVarInfo.VarKey + "_VarValue", lst),
                         varName,
                         strSuper + strDel
                        );
                    break;
                case "PSN":
                    returnstr = string.Format(
                         ltFormItem.Text,
                         strShowName,
                         FormAssist.Addtext(oSystemVarInfo.VarKey + "_VarValue", oSystemVarInfo.VarKey, oSystemVarInfo.VarValue, null) + GetPSNSelect(oSystemVarInfo.VarKey),
                         varName,
                         strSuper + strDel
                        );
                    break;
                case "PSNURL":
                    returnstr = string.Format(
                         ltFormItem.Text,
                         strShowName,
                         FormAssist.Addtext(oSystemVarInfo.VarKey + "_VarValue", oSystemVarInfo.VarKey, oSystemVarInfo.VarValue, null),
                         varName,
                         strSuper + strDel
                         );
                    break;
                default:
                    returnstr = string.Format(
                         ltFormItem.Text,
                         strShowName,
                         FormAssist.Addtext(oSystemVarInfo.VarKey + "_VarValue", oSystemVarInfo.VarKey + "_Id", oSystemVarInfo.VarValue, null),
                         varName,
                         strSuper + strDel
                         );
                    break;
            }
            return returnstr;
        }

        //加载所有参数类型的的链接页面
        public void LoadAllLink()
        {
            //初始化快捷链接
            foreach (ParamTypeInfo TempInfo in oParamTypeManage.GetListByCondition(0, "", "ParamTypeId"))
            {
                m_linkStr += " | " + "<a href=\"admin_setting.aspx?cid=" + TempInfo.ParamTypeId.ToString() + "\" >" + TempInfo.Name + "</a>";
            }
            m_linkStr += " | ";
        }

        /// <summary>
        /// 删除参数
        /// </summary>
        public void DelParam()
        {
            string action = RequestHelper.RequestString("action");
            if (action.IsEmpty())
                return;
            switch (action)
            {
                case "del":
                    string strKey = RequestHelper.RequestString("VarKey");

                    ParamTypeInfo oParamTypeInfo = new ParamTypeInfo();
                    oParamTypeInfo = oParamTypeManage.GetInfoById(RequestHelper.RequestInt("cid"));

                    SubmitAlert(SystemVarInfoHelper.DelVarbyKey(strKey, oParamTypeInfo.KeyWord));

                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// psn站点选择
        /// </summary>
        /// <param name="strid"></param>
        /// <returns></returns>
        public string GetPSNSelect(string strid)
        {
            if (strid.Contains("PSN"))
            {
                strid = strid.Remove(strid.LastIndexOf("PSN"));
            }
            else
                return "";
            return "<a href=" + "\"" +
                           "javascript:show_dialog('../AdminSelect.aspx?roottype=psn&extname=&setid=" + strid + "&selpath=',450,230,'选择资源发布点');" + "\"" +
                           ">选择目录</a>";
        }
        /// <summary>
        /// 是否添加删除选项
        /// </summary>
        /// <param name="ParamTypeId"></param>
        /// <param name="strkey"></param>
        /// <param name="strdelType"></param>
        /// <returns></returns>
        public string GetDelString(string ParamTypeId, string strkey, string strdelType)
        {
            if (strdelType.ToLower().Trim() == "false")
                return "";
            else
                return "|&nbsp;<a  href=\"admin_setting.aspx?cid=" + ParamTypeId + "&VarKey=" + strkey + " &action=del\" onclick=\"return confirm('你确定要删除变量吗？');\">删除</a>";
        }

        #endregion
    }
}
